package tichu_project252;

import java.util.LinkedList;
import tichu_project252.CardClasses.Card;

/**
 * Implements the basic discard pile of the table. This class has two basic
 * things. First the cards that the players drop to the table and the last
 * Dropped Cards.
 * 
* @author Alexandra Barka, ΑΜ: 2867, login : barka.
 * @author Euaggelia Petauraki AM: 2875 login: petavraki.
 * @version %I%, %G%
 */
public class Table {

    private LinkedList<Card> table;
    private LinkedList<Card> lastDropped;
    
    /**
     * Constructs a new discard pile.
     *
     * <p><b>Constructor: </b> Table()</p>
     *
     * <p><b>Precondition: </b> the deck has been dealt </p>
     * <p><b>Postcondition: </b> Constructs a new discard pile </p>
     */
    public Table() {
        table = new LinkedList<Card>();
        lastDropped = new LinkedList<Card>();
    }

    /**
     * Adds cards to table. <p> <b>Transformer(mutator):</b>
     * addCards(LinkedList<Card> played) </p>
     *
     * <p><b>Precondition: </b> The table has cards </p> <p><b>Postcondition:
     * </b>puts the last dropped cards in the table, and the new played in the
     * lastDropped </p>
     *
     * @param played The cards a player played.
     */
    public void addCards(LinkedList<Card> played) {
        elementFlush(lastDropped, table);
        elementFlush(played, lastDropped);

    }

    /**
     * Returns the last dropped cards.
     *
     * <p> <b> Accessor: </b> getLastDropped() </p>
     *
     * <p><b> Precondition: </b> The game has been started. </p> <p> <b>
     * Postcondition:</b> Returns the last dropped cards. </p>
     *
     * @return the last dropped cards
     */
    public LinkedList<Card> getLastDropped() {
        int phoenixRepr;
        if (lastDropped.size() == 1 && lastDropped.get(0).getstringRepr().equals("Phoenix")) {
            if (table.size() == 0) {
                phoenixRepr = 0;
                lastDropped.get(0).setintRepr(phoenixRepr);

            } else {
                phoenixRepr = table.get(table.size() - 1).getintRepr();
                lastDropped.get(0).setintRepr(phoenixRepr);
            }
        }
        return lastDropped;
    }

    private void elementFlush(LinkedList l1, LinkedList l2) {
        l2.addAll(l1);
        l1.clear();
    }

    /**
     * Adds the cards from the discard pile in the trick pile of a player.
     *
     * <p> <b>Transformer(mutator):</b>getTrick(LinkedList<Card> trick) </p>
     *
     * <p><b>Precondition: </b> The table has cards </p> <p><b>Postcondition:
     * </b>puts the last dropped cards in the table, and all the cards in
     * whoevers win the trick, trick pile. </p>
     *
     * @param trick The trick pile of a player.
     */
    public void getTrick(LinkedList<Card> trick) {
        elementFlush(lastDropped, table);
        elementFlush(table, trick);
    }
}
